package com.asiainfo.dacp.datastash.broker.core.vo;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 *
 * broker的信息
 *
 * @author MeiKefu
 * @date 2017-11-21
 */
@Data
@NoArgsConstructor
@ToString
public class Broker implements Serializable {

    private String brokerId;

    private String brokerIp;

    private String MaxNet;

    public Broker(String brokerId,String brokerIp){
        this.brokerId=brokerId;
        this.brokerIp=brokerIp;
    }

    /*
    heartbeat_dir = "/datastah/broker/heartbeat/"

//       单个盘100M计算
//        如果是10个盘做raid0(带条) 则为100*10
//        如果是10个盘做raid5       则为100*9
//        混合情况自己推算


    private String MaxIo;  
    // 实际宽带 值为 兆数/8 计算 1250M


    def get_survival(self):
        zk = KazooClient(hosts='192.168.1.203:2181')
        zk.start()
        return json.loads(zk.get(heartbeat_dir + brokerIp)[0])
        {"ip": "192.168.1.210", "MaxNet": 128, "MaxIo": 100}

    def get_monitoring(self):
        from redis import StrictRedis
        rd = StrictRedis(host='192.168.1.210',port=6379)

        boker = get_survival()
        if boker:
            mon = json.loads(rd.get("datastash:BrokerMonitoring:"+brokerIp)))
            // mon = {
            //     'idle_cpus': [{c[0]:c[1]._asdict()['idle']} for c in enumerate(cpu)],
            //     'io_Read': float(after_io.read_bytes - before_io.read_bytes)/interval/1024/1024,
            //     'io_Writes': float(after_io.write_bytes - before_io.write_bytes)/interval/1024/1024,
            //     'ip': LOCAL_IP,
            //     'mem_free': mem['free']/1024/1024,
            //     'mem_total': mem['total']/1024/1024,
            //     'net_Recv': float(after_net.bytes_recv-before_net.bytes_recv)/interval/1024/1024,
            //     'net_Sent': float(after_net.bytes_sent-before_net.bytes_sent)/interval/1024/1024,
            //     'stat_unixtimestamp': int(time.time()),
            //     'swap_free': sm.free/1024/1024,
            //     'swap_total': sm.total/1024/1024}
            _dct = {
                'ip':brokerIp,
                'io_rate':(mon['io_Read']+mon['io_Writes'])/boker['MaxIo'],
                'net_rate':(mon['net_Recv']+i['net_Recv'])/boker['MaxNet'],
                'mem_free': mon['mem_free'],
                'swap_free': mon['swap_free'],
                'cup_idle': sum([i.values()[0] for i in mon['idle_cpus']])
            }

            return _dct
    
    def chk_work_queue(self):
        from redis import StrictRedis
        rd = StrictRedis(host='192.168.1.210',port=6379)
        if len(rd.lrange("broker_"+ brokerIp +"_work_queue",0,-1))>0:
            return False
        return Ture

    def chk_resource(self):
        dct = get_monitoring()
        if dct['mem_free']>=100 and \
            dct['net_rate']<=0.6 and \
            dct['io_rate']<=1 and 
            dct['cup_idle']>150 and chk_work_queue()\

            return True

    */
}
